package com.salon.client.widgets;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.salon.client.GreetingServiceAsync;
import com.salon.client.Salon;

public class LoginDialog extends Dialog {

	  protected TextField<String> userName;
	  protected TextField<String> password;
	  protected Button reset;
	  protected Button login;
	  protected Status status;

	  public LoginDialog() {
	    FormLayout layout = new FormLayout();
	    layout.setLabelWidth(90);
	    layout.setDefaultWidth(155);
	    setLayout(layout);
	    
	    setButtonAlign(HorizontalAlignment.LEFT);
	    setButtons("");
	    setIcon(IconHelper.createStyle("user"));
	    setHeading("Авторизация");
	    setModal(true);
	    setBodyBorder(true);
	    setBodyStyle("padding: 8px;background: none");
	    setWidth(300);
	    setResizable(false);

	    KeyListener keyListener = new KeyListener() {
	      public void componentKeyPress(ComponentEvent event) {
	        if (validate()) {
	        	if (event.getKeyCode() == 13) {
	        		onSubmit();
	        	}
	        }
	      }
	    };
	       
	    Button regButton = new Button("Регистрация");
	    regButton.addSelectionListener(new SelectionListener<ButtonEvent>(){
	        public void componentSelected(ButtonEvent ce) {
	        	RegistrationDialog dlg = new RegistrationDialog();
	        	dlg.show();
	          }
	    });
	    this.getHeader().addTool(regButton);
	    
	    userName = new TextField<String>();
	    userName.setMinLength(4);
	    userName.setFieldLabel("Логин");
	    userName.addKeyListener(keyListener);
	    add(userName);

	    password = new TextField<String>();
	    password.setMinLength(4);
	    password.setPassword(true);
	    password.setFieldLabel("Пароль");
	    password.addKeyListener(keyListener);
	    add(password);

	    setFocusWidget(userName);
	  }

	  @Override
	  protected void createButtons() {
	    super.createButtons();
	    status = new Status();
	    status.setBusy("Идет загрузка...");
	    status.hide();
	    status.setAutoWidth(true);
	    getButtonBar().add(status);
	    
	    getButtonBar().add(new FillToolItem());
	    
	    reset = new Button("Сброс");
	    reset.addSelectionListener(new SelectionListener<ButtonEvent>() {
	      public void componentSelected(ButtonEvent ce) {
	        userName.reset();
	        password.reset();
	        validate();
	        userName.focus();
	      }

	    });

	    login = new Button("Войти");
	    login.disable();
	    login.addSelectionListener(new SelectionListener<ButtonEvent>() {
	      public void componentSelected(ButtonEvent ce) {
	        onSubmit();
	      }
	    });

	    addButton(reset);
	    addButton(login);
	  }

	  protected void onSubmit() {
	    status.show();
	    getButtonBar().disable();

	    ((GreetingServiceAsync) Registry.get(Salon.SERVICE)).
	    	login(userName.getValue(), password.getValue(), 
	    			new AsyncCallback<Void>() {

						@Override
						public void onFailure(Throwable caught) {
							Info.display("<font color=red>Неправильный логин/пароль</font>", "");
							status.hide();
							getButtonBar().enable();
							
						}

						@Override
						public void onSuccess(Void result) {
							LoginDialog.this.hide();
						}
	    		
	    	});
	  }

	  protected boolean hasValue(TextField<String> field) {
	    return field.getValue() != null && field.getValue().length() > 0;
	  }

	  protected boolean validate() {
		boolean allFieldsHasValue = hasValue(userName) && hasValue(password); 
	    login.setEnabled(allFieldsHasValue);
	    return allFieldsHasValue;
	  }

	}